def is_prime(n):
    if n < 2:
        return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return True

def find_largest_prime(num):
    if len(str(num)) != 4:
        return 0
    digits = [int(d) for d in str(num)]
    permutations = set()
    for i in range(4):
        for j in range(4):
            for k in range(4):
                for l in range(4):
                    if i != j and i != k and i != l and j != k and j != l and k != l:
                        permutations.add(digits[i] * 1000 + digits[j] * 100 + digits[k] * 10 + digits[l])
    largest_prime = 0
    for num in permutations:
        if is_prime(num) and num > largest_prime:
            largest_prime = num
    return largest_prime

num = int(input())
largest_prime = find_largest_prime(num)
print(largest_prime)